define(['jquery', 'bootstrap','download', 'backend', 'table', 'form','bootstrap-table-jumpto'], function ($, undefined, Download,Backend, Table, Form,showJumpto) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init();
            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.size() > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });

            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");


        },
        table: {
            first: function () {
                // 表格1
                var table1 = $("#table1");
                $.fn.bootstrapTable.locales[Table.defaults.locale].formatSearch = function(){return "姓名/身份证";};

                table1.on('common-search.bs.table', function (event, table) {
                    $('.my-create-file').addClass('disabled');
                });
                table1.on('load-success.bs.table', function (event, table) {
                    $('.my-create-file').removeClass('disabled').unbind('click').click(function () {
                        var options = table1.bootstrapTable('getOptions');
                        var search = options.queryParams({});
                        var filter = JSON.parse(search.filter);
                        var createtime = '';
                        var company_id = 0;
                        if (typeof filter.createtime !== "undefined") {
                            createtime = filter.createtime;
                        }
                        if (typeof filter.company_id !== "undefined") {
                            company_id = filter.company_id;
                        }

                        var url =  'zbtask/rate/exportXlsx?createtime=' + createtime + '&company_id=' + company_id;
                        Fast.api.open(url, '导出excel');
                    });

                });
                table1.bootstrapTable({
                    url: 'zbtask/rate/index' + location.search,
                    extend: {
                        index_url: 'zbtask/rate/index' + location.search,
                        add_url: 'zbtask/rate/add',
                        edit_url: 'zbtask/rate/edit',
                        del_url: 'zbtask/rate/del',
                        multi_url: 'zbtask/rate/multi',
                        table: 'zbtask_money_rate',
                    },
                    pk: 'id',
                    toolbar: '#toolbar1',
                    sortName: 'id',
                    showJumpto: true,
                    searchFormVisible: true,
                    showExport: false,
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('Id'),operate: false},
                            {field: 'user.real_name', title: '真实姓名',operate: 'like'},
                            {field: 'user.identity_card', title:'身份证', operate: 'like'},
                            {field: 'company.name', title:'公司名称', operate: false},
                            {field: 'company_id', title: '公司ID', addClass: "selectpage", extend: "data-source='company/index'  data-field='name' data-order-by='createtime desc'" , visible:false},
                            {field: 'money', title: __('Money'), operate: false},
                            {field: 'tax_rate', title: __('Tax_rate'), operate: false},
                            {field: 'ca_money', title: __('Ca_money'), operate: false,visible: false},
                            {field: 'ca_tax', title: __('Ca_tax'), operate: false, visible: false},
                            {field: 'kouchushu', title: __('Kouchushu'), operate: false},
                            {field: 'createtime', title: __('Createtime'), operate:'RANGE', extend: "data-date-format='YYYY-MM'" , addclass:'datetimepicker', formatter: Table.api.formatter.datetime},
                        ]
                    ]
                });


                // 为表格绑定事件
                Table.api.bindevent(table1);
            },
            second: function () {
                var table2 = $("#table2");
                $.fn.bootstrapTable.locales[Table.defaults.locale].formatSearch = function(){return "姓名/描述";};
                table2.bootstrapTable({
                    url: 'rate/file/index' + location.search,
                    extend: {
                        index_url: 'rate/file/index' + location.search,
                        add_url: 'rate/file/add',
                        edit_url: 'rate/file/edit',
                        del_url: 'rate/file/del',
                        multi_url: 'rate/file/multi',
                        table: 'rate_file',
                    },
                    pk: 'id',
                    toolbar: '#toolbar2',
                    sortName: 'id',
                    showJumpto: true,
                    searchFormVisible: true,
                    showExport: false,
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('Id'), operate: false},
                            {field: 'company.name', title: __('Company.name'), operate: false},
                            {field: 'name', title: __('Name'),  operate: 'like'},
                            {field: 'company_id', title: '公司ID', addClass: "selectpage", extend: "data-source='company/index'  data-field='name' data-order-by='createtime desc'" , visible:false},
                            {field: 'file', title: __('File'), operate: false},
                            {field: 'dec', title: __('Dec'), operate: 'like'},
                            {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                            {field: 'operate', title: __('Operate'), table: table2, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                               buttons: [
                                    {
                                        name: 'download',
                                        text: '下载',
                                        classname: 'btn btn-xs btn-warning btn-click',
                                        icon: 'fa fa-download',
                                        click: function (event, data) {
                                            window.open(data.file);
                                        }
                                    }
                               ],

                            }
                        ]
                    ]
                });
                // 为表格绑定事件
                Table.api.bindevent(table2);
            }
        },
        exportxlsx: function () {
            Form.api.bindevent($("form[role=form]"));
            $('.create-excel').click(function () {
                var type = $(this).data('type');
                var createtime = $("input[name='createtime']").val();
                var company_id = $("input[name='company_id']").val();
                if (!company_id) {
                    Fast.api.error('请选择公司');
                    return true;
                }
                if (!createtime) {
                    Fast.api.error('请选择要生成的月份');
                    return true;
                }
                if (type == 2) {
                    var url = 'zbtask/rate/exportXlsx?company_id='+ company_id + '&createtime='+createtime + '&type=' + type;
                    url = Fast.api.fixurl(url);
                    window.open(url);
                } else {
                    Fast.api.ajax({
                        url: 'zbtask/rate/exportXlsx',
                        data: {
                            company_id: company_id,
                            createtime: createtime
                        }
                    }, function (ret) {
                    });
                }
            });
        },
        add: function () {
            Controller.api.bindevent();
        },

        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});